﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CPPEI.ETL.Domain;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.IntegrityCheck
{
    [CPPEI.WebApi.AuthGetAction("IntegrityCheck", "GetInstanceList", "1.0")]
    public class GetInstanceListAction : BaseAction<GetInstanceListParams, GetInstanceListResult>
    {
        protected override ActionResult<GetInstanceListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetInstanceListParams opParams)
        {
            ListResult<CPPEI.ETL.Domain.Data.IntegrityCheckInstance> serResult = new SER.QueryService().QueryIntegrityCheckInstanceList(opParams.TaskId);

            if(serResult.IsSuccess)
            {
                GetInstanceListResult result = new GetInstanceListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetInstanceListResult.Item item = new GetInstanceListResult.Item();
                    item.Id = serResult.Data[index].Entity.Id;
                    item.IntegrityCheckId = serResult.Data[index].Entity.IntegrityCheckId;
                    item.IntegrityCheckName = serResult.Data[index].IntegrityCheckName;
                    item.IntegrityCheckDescription = serResult.Data[index].IntegrityCheckDescription;
                    item.ConditionText = serResult.Data[index].ConditionText;
                    result.Items.Add(item);
                }

                return new ActionResult<GetInstanceListResult>(result);
            }
            else
            {
                return new ActionResult<GetInstanceListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetInstanceListParams
    {
        public Guid TaskId { get; set; }
    }

    public class GetInstanceListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            /// <summary>
            /// 类型ID
            /// </summary>
            public Guid IntegrityCheckId { get; set; }
            /// <summary>
            /// 类型名称
            /// </summary>
            public string IntegrityCheckName { get; set; }
            /// <summary>
            /// 类型描述
            /// </summary>
            public string IntegrityCheckDescription { get; set; }

            public string ConditionText { get; set; }
        }
    }
}
